//
//  LGCheckPhotoTool.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/3/10.
//  查看大图

#import "LGCheckPhotoTool.h"
#define picTop      (statusBarHeight+viewPix(20))
#define picWidth    (Screen_W)
#define picHeight   (Screen_H-picTop-viewPix(60)-bottomSafeBarHeight/3.0)

#define itemWidth    viewPix(98)
#define itemHeight   viewPix(70)
@interface LGCheckPhotoTool ()<UIScrollViewDelegate>
/**   */
@property (nonatomic , strong)UIScrollView *scrollView;
/**   */
@property (nonatomic , strong)UIPageControl *pageControl;
/**   */
@property (nonatomic , strong)UILabel *countLabel;

@end

@implementation LGCheckPhotoTool

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.title = @"";
        self.hidden = YES;
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0];
        [self addSubview:self.scrollView];
//        [self addSubview:self.pageControl];
        [self addSubview:self.placeholderView];
        [self addSubview:self.countLabel];
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showViewAction)]];
    }
    return self;
}

-(void)setPicArray:(NSArray *)picArray{
    _picArray = picArray;
    self.pageControl.numberOfPages = picArray.count;
    for (UIImageView *imageView in self.scrollView.subviews) {
        [imageView removeFromSuperview];
    }
    for (NSInteger i=0; i<picArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(Screen_W*i, picTop, picWidth, picHeight)];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.tag = 1000+i;
        [self.scrollView addSubview:imageView];
        [imageView sd_setImageWithURL:[NSURL URLWithString:picArray[i]]];
    }
    self.scrollView.contentSize = CGSizeMake(Screen_W*picArray.count, Screen_H);
}

-(void)setPhotoArray:(NSArray *)photoArray{
    _photoArray = photoArray;
    self.pageControl.numberOfPages = photoArray.count;
    for (UIImageView *imageView in self.scrollView.subviews) {
        [imageView removeFromSuperview];
    }
    for (NSInteger i=0; i<photoArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(Screen_W*i, picTop, picWidth, picHeight)];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.image = photoArray[i];
        imageView.tag = 1000+i;
        [self.scrollView addSubview:imageView];
    }
    self.scrollView.contentSize = CGSizeMake(Screen_W*photoArray.count, Screen_H);
}

-(void)showViewAction{
    [self hidenViewAnimation:CGPointMake(Screen_W/2.0, Screen_H/2.0)];
}

-(void)showViewAnimation:(CGPoint)startPoint index:(NSInteger)index{
    NSArray *tempArray = self.picArray.count>0?self.picArray:self.photoArray;
    self.currentPage = index;
    self.pageControl.currentPage = index;
    self.countLabel.text = [NSString stringWithFormat:@"%@  %ld / %ld",self.title,index+1,tempArray.count];
    if (index<tempArray.count) {
        [self.scrollView setContentOffset:CGPointMake(Screen_W*index, 0)];
    }
    UIImageView *imageView = [self viewWithTag:1000+index];
    self.hidden = NO;
    self.pageControl.hidden = NO;
//    self.placeholderView.hidden = NO;
//    self.placeholderView.image = imageView.image;
//    self.placeholderView.contentMode = UIViewContentModeScaleAspectFit;
//    self.placeholderView.frame = CGRectMake(startPoint.x, startPoint.y, itemWidth, itemHeight);
    self.placeholderView.alpha = 0;
    [UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:1];
        self.placeholderView.alpha = 1;
//        self.placeholderView.frame = CGRectMake(0, picTop, picWidth, picHeight);
    } completion:^(BOOL finished) {
        self.scrollView.hidden = NO;
//        self.placeholderView.hidden = YES;
    }];
}

-(void)hidenViewAnimation:(CGPoint)endPoint{
    self.scrollView.hidden = YES;
    self.pageControl.hidden = YES;
//    self.placeholderView.hidden = NO;
//    UIImageView *imageView = [self viewWithTag:1000+self.currentPage];
//    self.placeholderView.image = imageView.image;
//    self.placeholderView.frame = CGRectMake(0, picTop, picWidth, picHeight);
    [UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
//        self.placeholderView.frame = CGRectMake(endPoint.x, endPoint.y, itemWidth, itemHeight);
        self.placeholderView.alpha = 0;
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0];
    } completion:^(BOOL finished) {
        self.hidden = YES;
        self.backgroundColor = [UIColor colorWithString:@"#000000"alpha:0];
        [self removeFromSuperview];
    }];
}



-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSArray *tempArray = self.picArray.count>0?self.picArray:self.photoArray;
    self.currentPage = scrollView.contentOffset.x/Screen_W;
//    self.pageControl.currentPage = self.currentPage;
    self.countLabel.text = [NSString stringWithFormat:@"%@  %ld / %ld",self.title,self.currentPage+1,tempArray.count];
}


-(UIScrollView *)scrollView{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.pagingEnabled = YES;
        _scrollView.hidden = YES;
        _scrollView.delegate = self;
    }
    return _scrollView;
}

-(UIImageView *)placeholderView{
    if(!_placeholderView){
        _placeholderView = [[UIImageView alloc]initWithFrame:CGRectMake(0, picTop, picWidth, picHeight)];
        _placeholderView.alpha = 0;
    }
    return _placeholderView;
}

-(UIPageControl *)pageControl{
    if(!_pageControl){
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(viewPix(20), Screen_H-viewPix(100), Screen_W-viewPix(40), viewPix(40))];
        _pageControl.pageIndicatorTintColor = [UIColor colorWithString:@"#333333"];
        _pageControl.currentPageIndicatorTintColor = [UIColor colorWithString:@"#DDDDDD"];
        _pageControl.hidden = YES;
    }
    return _pageControl;
}

-(UILabel *)countLabel{
    if(!_countLabel){
        _countLabel = [UILabel lableWithText:@"" colorString:@"#FFFFFF" textFont:LGFont(16) textAlignment:NSTextAlignmentCenter lines:1];
        _countLabel.frame = CGRectMake(viewPix(20), Screen_H-viewPix(50)-bottomSafeBarHeight/3.0, Screen_W-viewPix(40), 30);
    }
    return _countLabel;
}

@end
